package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linq.cool.beauty.domain.BeautyCartItem;
import com.linq.cool.beauty.domain.BeautyProduct;
import com.linq.cool.beauty.dto.BeautyCartItemDTO;
import com.linq.cool.beauty.service.BeautyCartItemService;
import com.linq.cool.beauty.service.BeautyProductService;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yqlin
 * @Date: 2021/2/8 22:23
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("cartItem")
public class BeautyCartItemController extends BaseController {
    @Autowired
    private BeautyCartItemService beautyCartItemService;
    @Autowired
    private BeautyProductService beautyProductService;

    /**
     * 添加客户购物车
     *
     * @return
     */
    @PostMapping("saveCart")
    public Result<?> saveCart(@RequestBody BeautyCartItemDTO dto) {
        return this.toResult(beautyCartItemService.saveCart(dto));
    }


    /**
     * 获取全部客户购物车所有商品信息
     *
     * @return
     */
    @GetMapping("getCartProducts")
    public Result<?> getCartProducts() {
        List<BeautyProduct> products = new ArrayList<>();
        List<BeautyCartItem> cartItems = beautyCartItemService.list(new LambdaQueryWrapper<BeautyCartItem>()
                                                                            .eq(BeautyCartItem::getMemberId, SecurityUtils.getUserId())
                                                                            .eq(BeautyCartItem::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                                            .eq(BeautyCartItem::getStatus, UserConstants.StateEnum.CART_ITEM_STATUS_1.getCode())
                                                                            .orderByDesc(BeautyCartItem::getGmtCreate)
        );
        if (CollectionUtils.isNotEmpty(cartItems)) {
            List<Long> productIds = cartItems.stream().map(BeautyCartItem::getProductId).collect(Collectors.toList());
            products = beautyProductService.listByIds(productIds);
        }
        return Result.ok(products);
    }

    /**
     * 获取当前微信登录用户购物车的商品数
     */
    @GetMapping("getProductCount")
    public Result<?> getProductCount() {
        log.info("获取当前客户的openId:{}", SecurityUtils.getOpenId());
        return Result.ok(beautyCartItemService.count(new LambdaQueryWrapper<BeautyCartItem>()
                                                             .eq(BeautyCartItem::getMemberId, SecurityUtils.getUserId())
        ));
    }

    /**
     * 查询当前客户购物车当前商品数量 也就是判断是不是已经存在存在返回数量
     *
     * @return
     */
    @GetMapping("count/{productId}")
    public Result<?> countByProductId(@PathVariable("productId") Long productId) {
        return Result.ok(beautyCartItemService.count(new LambdaQueryWrapper<BeautyCartItem>()
                                                             .eq(BeautyCartItem::getProductId, productId)
                                                             .eq(BeautyCartItem::getMemberId, SecurityUtils.getUserId())
        ));
    }

    /**
     * 分页查询当前客户的购物车
     *
     * @param current 当前页
     * @param size    每页显示数量
     *
     * @return
     */
    @GetMapping("list/{current}/{size}")
    public Result<?> list(@PathVariable("current") long current, @PathVariable("size") long size) {
        IPage<BeautyCartItem> page = beautyCartItemService.page(new Page<BeautyCartItem>().setCurrent(current).setSize(size), new LambdaQueryWrapper<BeautyCartItem>()
                .eq(BeautyCartItem::getMemberId, SecurityUtils.getUserId())
                .eq(BeautyCartItem::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                .eq(BeautyCartItem::getStatus, UserConstants.StateEnum.CART_ITEM_STATUS_1.getCode())
                .orderByDesc(BeautyCartItem::getGmtCreate)
        );
        return Result.ok(page.getRecords());
    }

    /**
     * 批量删除购物车
     */
    @DeleteMapping("remove/{cartItemIds}")
    public Result<?> remove(@PathVariable("cartItemIds") List<Long> cartItemIds) {
        return this.toResult(beautyCartItemService.removeByIds(cartItemIds));
    }

    /**
     * 根据商品id移除购物车
     *
     * @param productId
     *
     * @return
     */
    @DeleteMapping("removeCart/{productId}")
    public Result<?> removeCart(@PathVariable("productId") Long productId) {
        return this.toResult(beautyCartItemService.remove(new LambdaQueryWrapper<BeautyCartItem>()
                                                                  .eq(BeautyCartItem::getProductId, productId)
        ));
    }


    /**
     * 移入收藏
     *
     * @return
     */
    @PostMapping("removeToCollection/{cartItemIds}")
    public Result<?> removeToCollection(@PathVariable("cartItemIds") List<Long> cartItemIds) {
        beautyCartItemService.removeToCollection(cartItemIds);
        return Result.ok("移入成功");
    }
}
